//
//  ViewController.m
//  HYApp
//
//  Created by Sunline on 2018/11/14.
//  Copyright © 2018 Unidax. All rights reserved.
//

#import "ViewController.h"
#import "HYApp-Swift.h"
#import "ChangeVerifyView.h"
#import "AWUserInfo.h"

@interface ViewController ()
@property (nonatomic, assign) BOOL isRequest;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
 
    
    
}
-(void)autoLogin{
    self.isRequest = YES;
    WEAKSELF
    if ([HYNetHelper isLogined]) {
        return;
    }
    A0WebAuth * webAuth = [[A0WebAuth alloc] init];
    [webAuth setScope:@"openid profile email"];
    [webAuth start:^(NSError * error, A0Credentials * data) {
        if (!error) {
            [HYNetHelper setAuthToken:data.idToken];
            [HYNetHelper setLogined:YES];
            AWDispatch_main_async_safe(^{
              [SLRemindView showHUDAddedTo:weakSelf.view animated:YES];
            })

            [HYNetHelper auth0auth:data.accessToken appId:[HYNetHelper appid] tfaToken:@""  verifyType:@"2"  callBack:^(JFResultVo *resultVo) {
               
                if (resultVo.errorNo == 0) {
                    [HYNetHelper setAuthToken:resultVo.results[@"token"]];
                    [HYNetHelper allUsers:^(JFResultVo *resultVo) {
                        [SLRemindView hideHUDForView:weakSelf.view animated:YES];
                        if (resultVo.errorNo == 0) {
                            [User() initWithDic:resultVo.results];
                            User().mobile = resultVo.results[@"mobile"];
                            [[NSNotificationCenter defaultCenter]postNotificationName:@"changeRoot" object:nil];
                        }
                    }];
                }else{
                    [SLRemindView hideHUDForView:weakSelf.view animated:YES];
                    NSDictionary *dataDict = resultVo.results[@"data"];
                    NSArray * available2faMethods  = dataDict[@"message"][@"available2faMethods"];
                    User().available2faMethods = available2faMethods;
                    [weakSelf showVyAlertAccessToken:data.accessToken];
                }
            }];
        }else{
            weakSelf.isRequest = NO;
        }
    }];
}
-(void)showVyAlertAccessToken:(NSString*)accessToken{
    ChangeVerifyView *view = [[[NSBundle mainBundle] loadNibNamed:@"ChangeVerifyView" owner:nil options:nil] firstObject];
    view.frame = CGRectMake(0, 0, CUSTOMVIEW_WIDTH, 110);
    view.isLogin = YES;
    view.accessToken = accessToken;
    WEAKSELF
    FZAlertController *vc = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"Captcha_ico_b"] title:nil customView:view message:nil];
    vc.showCloseBtn = YES;
    FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"confirm") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
         NSString * token  = view.codeText.text;
        [SLRemindView showHUDAddedTo:weakSelf.view animated:YES];
        [HYNetHelper auth0auth:accessToken appId:[HYNetHelper appid] tfaToken:token  verifyType:view.verifyType  callBack:^(JFResultVo *resultVo) {
            
            if (resultVo.errorNo == 0) {
                [HYNetHelper setAuthToken:resultVo.results[@"token"]];
                
                 [HYNetHelper allUsers:^(JFResultVo *resultVo) {
                     [SLRemindView hideHUDForView:weakSelf.view animated:YES];
                     if (resultVo.errorNo == 0) {
                         [User() initWithDic:resultVo.results];
                         User().mobile = resultVo.results[@"mobile"];
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeRoot" object:nil];
                     }
                 }];
                
             
            }else{
                 [SLRemindView hideHUDForView:weakSelf.view animated:YES];
            }
        }];
    }];
    [vc addAction:action];
    [self presentViewController:vc animated:YES completion:nil];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (!self.isRequest) {
      [self autoLogin];
    }
  
}
@end
